package top.infopub.mgr.hr.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.hr.dao.DeptInfoDao;
import top.infopub.mgr.hr.dao.PostInfoDao;
import top.infopub.mgr.hr.service.DeptInfoService;
import top.infopub.mgr.hr.vo.DeptInfoVo;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.hr.DeptInfo;
import top.infopub.security.util.SessionUserUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class DeptInfoServiceImpl extends GenericServiceImpl<DeptInfo, String> implements
		DeptInfoService {

	@Autowired
	private DeptInfoDao deptInfoDao;

	@Autowired
	private PostInfoDao postInfoDao;

	@Override
	public GenericDao<DeptInfo, String> getDao() {
		return deptInfoDao;
	}

    @Override
    public Page<DeptInfoVo> selectPageByDomainWhere(Page<DeptInfoVo> page, DeptInfoVo deptInfoVo) {

        //处理时间范围
        if (!StringUtils.isEmpty(deptInfoVo.getDates())) {
            String[] splitTime = deptInfoVo.getDates().split("~");
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            try {
                deptInfoVo.setEndDate(format.parse(splitTime[1]));
                deptInfoVo.setStartDate(format.parse(splitTime[0]));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<DeptInfoVo>)deptInfoDao.selectByDomainWhere(deptInfoVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDeptInfo(DeptInfoVo deptInfoVo, boolean flag) {
        if(flag){
            if(StringUtils.isEmpty(deptInfoVo.getParentId())){
                deptInfoVo.setParentId("-1");
            }
            deptInfoVo.setDeptName(deptInfoVo.getDeptName().trim());
            deptInfoVo.setState(1);// 默认是1=启用
            deptInfoVo.setAddBy(SessionUserUtils.getSessionUserId());
            deptInfoVo.setAddTime(DateUtils.getCurrentDateTime());
            deptInfoVo.setUptBy(SessionUserUtils.getSessionUserId());
            deptInfoVo.setUptTime(DateUtils.getCurrentDateTime());
            deptInfoDao.insertById(deptInfoVo);
        }else{
            deptInfoVo.setDeptName(deptInfoVo.getDeptName().trim());
            DeptInfo deptInfoUpdate = deptInfoDao.selectByPrimaryKey(deptInfoVo.getId());
            BeanUtils.copyProperties(deptInfoVo, deptInfoUpdate);
            deptInfoUpdate.setUptTime(new Date());
            deptInfoUpdate.setUptBy(SessionUserUtils.getSessionUserId());
            this.updateBySelective(deptInfoUpdate);
        }
    }

    @Override
    public List<DeptInfo> selectByOrgId(String orgId) {
        return deptInfoDao.selectByOrgId(orgId);
    }

    @Override
    public List<DeptInfo> selectAllByOrgId(String orgId) {
        return deptInfoDao.selectAllByOrgId(orgId);
    }

    @Override
    public List<DeptInfo> selectByDeptNameAndId(String deptName, String id, String orgId) {
        return deptInfoDao.selectByDeptNameAndId(deptName,id,orgId);
    }

}
